import axios from "axios";
import { Message } from 'element-ui';
import {initMenus} from "@/utils/menus";
import router from "@/router";
import store from "@/store";
axios.interceptors.response.use(success=>{
        if (success.status && success.status ==200 && success.data.status == 500){
            Message.error(success.data.msg);
            return;
        }

        if (success.data.msg){
            Message.success(success.data.msg)
        }
        return success.data;
},error => {
        if (error.response.status == 504 || error.response.status == 404){
            Message.error('请求的资源不存在！');
        }else if (error.response.status == 403){
            Message.error('权限不足,请联系管理员');
        }else if (error.response.status == 401){
            Message.error('尚未登录,请登录');
        }else {
            if (error.response.data.msg){
                Message.error('尚未登录,请登录');
            }else {
                Message.error('未知异常');
            }
        }
    }
)

let base='';
export const KeyValuePostRequest=(url,params)=>{
    return axios({
        method:'post',
        url:`${base}${url}`,
        data:params,
        transformRequest:[function (data){
            let ret ='';
            for (let i in data){
                ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + '&';
            }
            return ret;
        }],
        headers:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    })
};

export const postRequest = (url,params)=>{
    return axios({
        method:'post',
        url:`${base}${url}`,
        data:params
    })
}

export const putRequest =(url,params)=>{
    return axios({
        method:'put',
        url:`${base}${url}`,
        data:params
    })
}

export const getRequest=(url,params)=>{
    return axios({
        method:'get',
        url:`${base}${url}`,
        data:params
    })
}

export const deleteRequest=(url,params)=>{
    return axios({
        method:'delete',
        url:`${base}${url}`,
        data:params
    })
}

